/*
 * Public libusb header file
 * Copyright (c) 2010 Chen Wang  <cnnbboy@163.com>
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <QDebug>
#include <QFile>

#include "QDnwConfigurationManager.h"

QDnwConfigurationManager::QDnwConfigurationManager(QObject *parent) :
    QObject(parent)
{
    this->configFile = new QFile(":/smdk.conf", this);
}

QDnwConfigurationManager::~QDnwConfigurationManager()
{
    this->configFile->close();
    QDnwConfigurationManager_clearList();
}

void QDnwConfigurationManager::QDnwConfigurationManager_ReadConfiguration()
{
    if (!configFile->open( QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "Open Configuration file failed";
        return ;
    }
    bool ok;
    QTextStream in(configFile);
    while (!in.atEnd()) {
        QDnwSmdkType *smkdType = new QDnwSmdkType;
        for(int i = 0;i < 3;i++) {
            QString line = in.readLine();
            if ( 0 == i){
                line = line.mid(line.indexOf(':') + 1);
                smkdType->socType = line;
                qDebug() << smkdType->socType;
            }
            else if ( 1 == i) {
                line = line.mid(line.indexOf(':') + 1);
                qDebug() << line;
                smkdType->vendorId = line.toInt(&ok, 16);
                qDebug() << smkdType->vendorId;
            }
            else if ( 2 == i) {
                line = line.mid(line.indexOf(':') + 1);
                qDebug() << line;
                smkdType->productId = line.toInt(&ok, 16);
                qDebug() << smkdType->productId;
            }
        }
        configList.push_back( smkdType );
    }
}


void QDnwConfigurationManager::QDnwConfigurationManager_getSmdaType(const QString &socType, QDnwSmdkType *now)
{
    int len = this->configList.length();
    for (int i = 0 ;i < len;i++) {
        if ( socType == configList[i]->socType ) {
            now = configList[i];
            break;
        }
    }
}

void QDnwConfigurationManager::QDnwConfigurationManager_getList(QList<QDnwSmdkType *> *list)
{

}

void QDnwConfigurationManager::QDnwConfigurationManager_clearList()
{
    int len = this->configList.size();
    for (int i = 0; i < len;i++){
        delete configList[i];
    }
    configList.clear();
}

void QDnwConfigurationManager::debugList()
{
    int len = this->configList.size();
    for (int i = 0; i < len;i++) {
        qDebug() << "Debug";
        qDebug() << configList[i]->socType << configList[i]->vendorId << configList[i]->productId;
    }
}
